package com.skyline.attraction.controller;

/**
 * @author Colonel
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.skyline.attraction.model.Suggestion;
import com.skyline.attraction.service.AttractionInviteService;
import com.skyline.base.service.NoticeService;
import com.skyline.base.type.NotificationType;
import com.skyline.common.bean.Page;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.Fan;
import com.skyline.user.model.User;
import com.skyline.user.service.FanService;

@RequestMapping("/attraction")
@Controller
public class AttrationInviteController {

//	@Value("${view.spot.invite}")
//	private String inviteView;

	@Autowired
	private FanService attentionService;
	
	@Autowired
	private NoticeService noticeService;
//
//	@Value("${view.wo.myWo}")
//	private String myWoView;

	@Autowired
	private AttractionInviteService inviteService;

	@RequestMapping(value = "/invite", method = RequestMethod.GET)
	public ModelAndView inviteFriends() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName(ViewPaths.SPOT_INVITE);

		User user = WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		long userId = user.getId();

		Page page = new Page();
		page.setCurpage(5);
		page.setSize(20);
		page.setTotal(120);

		ArrayList<Fan> fanList = null;
		fanList = (ArrayList<Fan>) attentionService.listFans(userId, page);
		WebHelper.setSessionAttribute(null, Constant.SESSION_FANS, fanList);
		mav.addObject("fanList", fanList);

		return mav;
	}

	/**
	 * 正在完善中...
	 * 
	 * @param destination
	 *            地点
	 * @param appointmentTime
	 *            预约时间
	 * @param fanInvitedList
	 *            受邀的好友列表
	 * @param inviteContent
	 *            邀请的内容
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/invite", method = RequestMethod.POST)
	public ModelAndView inviteFriends(String destination,
			String appointmentTime, String fanIdList, String fanNicknameList,
			String fanPortraitList, String inviteContent) throws ParseException {
		ModelAndView mav = new ModelAndView();
		mav.setViewName(ViewPaths.WO_MYWO);

		SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); 
		Date appTime = sdf.parse(appointmentTime);
		
		// 取得当前user的ID；
		User user = WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		long userId = user.getId();

		//得到受到邀请的粉丝人数
		int number = 0;
		String[] idListArr = fanIdList.split(";");
		String[] nicknameArr = fanNicknameList.split(";");
		String[] portraitArr = fanPortraitList.split(";");

		for (String idStr : idListArr) {
			if (idStr == "-1" || idStr.equals("-1")) {
				break;
			}
			number++;
		}

		for (int i = 0; i < number; i++) {
			long fanId = Integer.parseInt(idListArr[i]);
			String fanNickname = nicknameArr[i];
			String fanPortait = portraitArr[i];
			
			//临时数据
			long spotId = 1;
			String spotName = "浙大玉泉";
			
			//获取插入invite中返回的ID， 给相应的粉丝发送通知
			long notificationId = inviteService.addInvitation(userId, fanId, fanNickname, fanPortait,
					spotId, spotName, inviteContent, appTime);
			noticeService.addNotication(fanId, userId, user.getNickname(), String.valueOf(notificationId), "邀请", NotificationType.INVITATION);
			
		}

		return mav;
	}
	
	
	//返回autocomplete的数据
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/invite/getSuggestion")
	public @ResponseBody Suggestion getSuggestion(String query){
		
		Suggestion sgg = new Suggestion();
		sgg.setQuery(query);
		
		ArrayList<Fan> fanList = null;
		fanList = (ArrayList<Fan>)WebHelper.getSessionAttribute(null, Constant.SESSION_FANS);
		List<String> suggs = new ArrayList<String>();
		List<String> datas = new ArrayList<String>();
		
		for(Fan fan:fanList){
			//忽略大小写 首字母匹配
			if(fan.getFanNickname().toLowerCase().indexOf(query.toLowerCase()) == 0){
				suggs.add(fan.getFanNickname());
				datas.add(fan.getFanId()+","+fan.getFanNickname()+","+fan.getFanPortrait());
			}
		}
		sgg.setSuggestions(suggs);
		sgg.setData(datas);		
		
		return sgg;
	}
	

}
